<template>
	<view class="content">
		<view class="page-area">
			<view class="tabsBox">
				<u-tabs :list="list1" :current='currentIndex' @click="click" lineColor="#fff"
				:activeStyle="{
					color: '#191919', 
					fontSize:'14px',
					fontWeight: 'bold',
				}"
				:inactiveStyle="{
					fontSize:'14px',
					color: '#828282', 
				}"></u-tabs>
			</view>
			<view class="filterBox">
				<view class="left">
					<view class="item" :class="{active:sort==''}" @click="handleToSarch('')">
						综合
					</view>
					<view class="item" :class="{active:sort=='2'}" @click="handleToSarch('2')">
						销量
					</view>
					<view class="item"  :class="{active:sort=='3' || sort=='4'}"  @click="handlePriceSarch()">
						价格
						<view class="icon">
							<u-icon name="arrow-up" :color="sort=='4'?'#000':'#999'" size="6" ></u-icon>
							<u-icon name="arrow-down"  :color="sort=='3'?'#000':'#999'" size="6"></u-icon>
						</view>
					</view>
				</view>
				  <view class="right">
					<view class="item" @click="handleChangeCollect">
						{{isCollect?'已上架':'未上架'}}
					</view>
				</view>  
			</view>
		</view>
		<view v-if="list.length" class="goods_list_box">
			<view class="goods_list">
				<view class="item" v-for="item in list" :key="item.id" @click="handleToDetail(item.id)">
					<view class="image">
						<image :src="item.pic" mode=""></image>
					</view>
					<view class="text">
						<view class="title clamp1">
							{{item.name}}
						</view>
						<view class="dec clamp1">
							{{item.subTitle}}
						</view>
						<view class="price">
							￥{{item.price}}
							<text v-if="item.commission">(佣金￥{{item.commission}})</text>
						</view>
						<view class="btn" v-if="item.isCollect == 0" @click.stop="handleChangeAdd(item.id)">
							上架
						</view>
						<view class="btn" v-else @click.stop="handleChangeDel(item.id)">
							下架
						</view>
					</view>
				</view> 
			</view>
			 <u-loadmore class="loadmore" :status="status" v-if="pageNum > 1" /> 
		</view>
		
		
		<view class="null" v-else>
			<u-empty text="暂无商品" icon="/static/e.png" marginTop="100" ></u-empty>
		</view> 
	</view>
</template>

<script>
	export default{
		data(){
			return{
				status: 'loadmore', 
				allData: false,
				list1: [],
				sort:'',
				productCategoryId:'',
				type:'',
				list:[],
				isCollect:0,
				parentId:'',
				pageNum:1,
				currentIndex:0,
			}
		},
		onLoad(option) { 
		    this.currentIndex = Number(option.index)
			this.productCategoryId = option.id
			this.parentId = option.parentId
			this.type = option.type
			this.getCateList()
			this.getList()
		},
		
		onReachBottom() {
			if (this.allData) {
				this.status = 'loading'
				this.pageNum++
				this.getList();
			}
		},
		computed: {
			userInfo() {
				return this.$store.state.userInfo 
			}
		},
		methods: { 
			handleChangeDel(id){
				this.$http({
					url:"/pmsStoreProductRelation/deleteById",
					method:"get",
					data:{
						id
					}
				}).then(data=>{
					uni.showToast({
						title:"下架成功"
					})
					const index = this.list.findIndex(item=>item.id == id)
					this.list[index].isCollect = 0
				})
			},
			handleChangeAdd(id){
				this.$http({
					url:"/pmsStoreProductRelation/add",
					method:"post",
					data:{
						memberId:this.userInfo.id,
						productId:id
					}
				}).then(data=>{
					uni.showToast({
						title:"上架成功"
					})
					const index = this.list.findIndex(item=>item.id == id)
					this.list[index].isCollect = 1
				})
			},
			handlePriceSarch(){
				if(this.sort !='3' && this.sort!='4'){
					this.sort = '3'
				}else if(this.sort =='3'){
					this.sort = '4'
				}else if(this.sort == '4'){
					this.sort = '3'
				} 
				this.pageNum = 1
				this.getList()
			},
			handleToSarch(type){
				this.sort = type
				this.pageNum = 1
				this.getList()
			},
			getCateList(){
				this.$http({
					url:`/home/productCateList/${this.parentId}`,
					method:"get"
				}).then(data=>{  
					this.list1 = data
				})
			},
			handleChangeCollect(){
				this.isCollect = this.isCollect == 1 ? 0 : 1; 
				this.pageNum = 1
				this.getList()
			},
			handleToDetail(id){
				uni.navigateTo({
					url:`./detail?id=${id}`
				})
			},
			click(item) {
				this.productCategoryId = item.id
				this.pageNum = 1
				this.getList()
			},
			getList(){
				this.$http({
					url:"/product/search",
					method:"get",
					data:{
						storeId:this.userInfo.id,
						sort:this.sort,
						productCategoryId:this.productCategoryId,
						type:this.type,
						isCollect:this.isCollect,
						pageNum:this.pageNum,
						pageSize:10,
					}
				}).then(data=>{
					if (this.pageNum == 1) {
						this.list = data.list
					} else {
						this.list.push(...data.list) 
					}  
					if (data.totalPage > data.pageNum) {
						this.allData = true
						this.status = 'loading'
					} else {
						this.status = 'nomore' 
					    this.allData = false
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.cart_icon{
		position: fixed;
		right: 30rpx;
		bottom: 60rpx;
		image{
			width: 88rpx;
			height: 88rpx;
		}
	}
	.goods_list_box{
		padding-bottom: 30rpx;
	}
	.goods_list{
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		padding: 24rpx;
		padding-bottom: 0;
		box-sizing: border-box;
		.item{
			width: 49%;
			margin-bottom: 20rpx;
			background: #F8F8F8;
			 margin-bottom: 22rpx;
			 padding-bottom: 32rpx;
			.image{
				width: 100%;
				height: 310rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				position: relative;
				.t{
					position: absolute;
					right: 16rpx;
					top: 16rpx;
				}
				image{
					width: 100%;
					height: 310rpx;
				}
			}
			.text{
				padding: 0 30rpx;
				padding-top: 20rpx;
				.title{
					font-size: 28rpx;
					color: #333;
					text-align: center;
				}
				.dec{
					font-size: 24rpx;
					color: #999;
					margin-top: 8rpx;
					text-align: center;
				}
				.price{
					font-size: 28rpx;
					margin-top: 16rpx;
					text-align: center;
					display: flex;
					align-items: center;
					justify-content: center;
					color: #333;
					text{
						font-size: 24rpx;
						color: #666;
						margin-left: 4rpx;
					}
				}
				.btn{
					width: 108rpx;
					height: 50rpx;
					background: #020202;
					border-radius: 30rpx;
					line-height: 50rpx;
					text-align: center;
					color: #fff;
					font-size: 22rpx;
					margin: 0 auto;
					margin-top: 18rpx;
				}
			}
		}
	}
	.filterBox{
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 40rpx;
		.right{
			display: flex;
			align-items: center;
			.item{
				width: 110rpx;
				height: 48rpx;
				font-size: 24rpx;
				color: #fff;
				background: #020202;
				margin-right: 24rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				border-radius: 25rpx;
				border: 1rpx solid #020202;
			}
			text{
				font-size: 24rpx;
				color: #020202;
			}
			image{
				width: 22rpx;
				height: 22rpx;
				margin-left: 10rpx;
			}
		}
		.left{
			display: flex;
			align-items: center;
			.item{
				width: 98rpx;
				height: 48rpx;
				font-size: 24rpx;
				color: #999;
				margin-right: 24rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				border-radius: 25rpx;
				border: 1rpx solid #E8E8E8;
				&.active{
					border: 1px solid #333;
					color: #333;
				}
				.icon{
					margin-left: 4rpx;
				}
			}
		}
	}
	.tabsBox{
		border-bottom: 1rpx solid #E8E8E8;
		padding-bottom: 20rpx;
	}
	.page-area{
		    position: sticky;
		    top: 0;
		    background: #fff;
		    z-index: 99;
		    left: 0;
		padding: 24rpx;
		box-sizing: border-box;
	}
</style>